/**********
    @name: depart科室选择组件
    @describtion:
    @author:liubingye
    @date:2023-03-03 11:18:10
***********/
<template>
  <div class="depart-outer-box">
    <div class="editor-icon" v-if="props.children && props.children.length > 0" @click="changeChildrenShow">
      <span :class="{ plus: childrenShow }"></span>
    </div>
    <el-form-item class="ruifox-depart" :label="props.title" :prop="props.field">
      <el-popover
          :visible="popoverVisible"
          trigger="focus"
          :width="700"
          placement="bottom"
      >
        <div class="inner-depart-option">
          <div class="clear-all">
            <el-button type="primary" link @click="clearChoosed">清空</el-button>
            <el-button type="primary" link @click="closePopover">确定</el-button>
          </div>
          <el-tabs v-model="activeName" @tab-change="handleTabChange">
            <el-tab-pane v-for="item in areaDepart" :label="item.name" :key="item.code" :name="item.code">
              <el-scrollbar height="300px">
                <div class="depart-wrap" v-for="(departList, pinyin, index) in item.depart_class" :key="index">
                  <h4 class="depart-class-title">{{ pinyin }}</h4>
                  <div class="depart-class-list">
                    <span class="depart" :class="{ 'is-chose': judgeChose(depart) }" v-for="depart in departList" :key="depart.id" @click="handleChooseDepart(depart)">{{ depart.name }}</span>
                  </div>
                </div>
              </el-scrollbar>
            </el-tab-pane>
          </el-tabs>
        </div>
        <template #reference>
          <div ref="popReference" :class="{'is-focus': selectFocus}" class="depart-select" tabindex="-1" @focus="handleFocusDepart" @blur="handleBlurDepart">
            <div class="depart-container">
            <span class="chose-depart" v-for="item in choseDepartList" :key="item.id">
              {{item.name}}
              <el-icon @click="handleCancel(item)"><Close /></el-icon>
            </span>
            </div>
            <div class="choose-depart">
              <span v-if="props.title">选择{{props.title}}</span>
              <span v-else>选择科室</span>
              <i class="iconfont">&#xe678;</i>
            </div>
          </div>
        </template>
      </el-popover>
    </el-form-item>
  </div>
</template>

<script setup>
import { ref, computed } from "vue"
import { Close } from '@element-plus/icons-vue'
import { selectDepart } from '@/api/category'
import { selectListDict } from "@/api/dictionary"
import { useStore } from "vuex"

const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  value: {
    type: String,
    default: null
  },
  field: {
    type: String,
    default: ''
  },
  children: {
    type: Array,
    default: () => []
  },
  childrenShow: {
    type: Boolean,
    default: false,
  }
})
const store = useStore()
const popReference = ref()
const popoverVisible = ref(false)
const choseDepartList = computed({
  get: () => props.value ? JSON.parse(props.value) : [],
  set(val) {
    emits('update:value', JSON.stringify(val))
  }
})
const emits = defineEmits(['update:value', 'update:childrenShow'])
const siteId = computed(() => store.state.site.currentSite?.id)

const selectFocus = ref(false)
const childrenShow = computed(() => props.childrenShow)
const changeChildrenShow= () => {
  emits("update:childrenShow", !childrenShow.value)
}
// 获取科室列表
const activeName = ref(null)
const areaDepart = ref([])
const getAreaDepart = async () => {
  const res = await selectListDict({ dictionaryGroupId: 7 })
  const res2 = await selectDepart({ siteId: siteId.value })
  if (res.code === 20000) {
    res.data.forEach(item => {
      const temp = JSON.parse(item.dataValue)
      const area = {
        name: temp.name,
        code: temp.code,
        depart_class: res2.data[temp.code]
      }
      areaDepart.value.push(area)
    })
  }
  activeName.value = areaDepart.value[0]?.code
}
getAreaDepart()

const handleFocusDepart = () => {
  selectFocus.value = true
  popoverVisible.value = true
}
const handleTabChange = () => {
  popReference.value.focus()
}
const handleBlurDepart = () => {
  selectFocus.value = false
  popoverVisible.value = false
}
const handleChooseDepart = (depart) => {
  popReference.value.focus()
  const index = choseDepartList.value.findIndex(item => item.id === depart.id)
  if(index === -1){
    const {id, name, info, uri} = depart
    const jsonInfo = JSON.parse(info)
    const newDepart = {id, name, ...jsonInfo, uri}
    choseDepartList.value =[...choseDepartList.value, newDepart]
  }else{
    choseDepartList.value.splice(index, 1)
  }
}
const judgeChose = (depart) => {
  return choseDepartList.value.some((item) => {return item.id === depart.id})
}
const handleCancel = (depart) => {
  popoverVisible.value = false
  popReference.value.blur()
  choseDepartList.value = choseDepartList.value.filter(item => item.id != depart.id)
}
const clearChoosed = () => {
  choseDepartList.value = []
}
const closePopover = () => {
  popoverVisible.value = false
}
</script>

<style scoped lang="scss">
.depart-outer-box{
  min-height: 56px;
  box-sizing: border-box;
  border-bottom: 1px solid #E4E4E4;
  display: flex;
  align-items: center;
  ::v-deep(.el-form-item){
    align-items: center;
    margin-bottom: 0;
    width: 100%;
  }
}
.depart-select{
  min-height: 42px;
  width: 100%;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0 10px;
  .choose-depart{
    display: flex;
    flex-shrink: 0;
    font-size: 14px;
    span{
      color: $mainColor;
    }
    i{
      margin-left: 10px;
      font-size: 16px;
      color: #CACED4;
    }
  }
  .depart-container{
    display: flex;
    flex-wrap: wrap;
    min-height: 40px;
    .chose-depart{
      display: flex;
      align-items: center;
      padding: 0 5px;
      height: 20px;
      line-height: 20px;
      margin: 10px;
      font-size: 14px;
      border-radius: 5px;
      background-color: #f0f2f5;
      cursor: default;
      i{
        cursor: pointer;
        margin-left: 10px;
      }
    }
  }
}
.depart-wrap{
  display: flex;
  .depart-class-title{
    color: $mainColor;
    width: 20px;
    flex-shrink: 0;
  }
  .depart-class-list{
    display: flex;
    flex-wrap: wrap;
    span.depart{
      display: block;
      padding: 0 5px;
      height: 20px;
      line-height: 21px;
      margin: 0 10px 10px 0;
      border-radius: 5px;
      cursor: pointer;
      &:hover{
        background-color: $borderColor;
      }
    }
    span.depart.is-chose{
      background-color: $mainColor!important;
      color: $white;
    }
  }
}
</style>
